﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Logic;
using Entities;

namespace Presentation
{
    public enum TagsChangedType{
        Add,
        Set,
        Remove
    }

    public partial class TagBox : FlowLayoutPanel
    {
        private TagControlCollection tags;

        public TagControlCollection Tags
        {
            get { return tags; }
        }

        public event EventHandler TagsChanged;

        protected virtual void OnTagsChanged(EventArgs e, TagsChangedType type)
        {
            if (this.TagsChanged != null)
                this.TagsChanged(this, e);

            switch (type)
            {
                case TagsChangedType.Add:
                    Controls.Add(tags[tags.Count - 1]);
                    break;
                case TagsChangedType.Set:
                    for (int i = 0; i < Controls.Count; i++)
                    {
                        if ((Controls[i] is TagControl))
                        {
                            Controls.RemoveAt(i);
                        }
                    }

                    Controls.AddRange(tags.ToArray());
                    break;
            }
        }

        protected virtual void OnTagsChanged(EventArgs e)
        {
           OnTagsChanged(new EventArgs(), TagsChangedType.Set);
        }

        public TagBox()
        {
            this.tags = new TagControlCollection(this);
            InitializeComponent();
        }

        #region Methods

        public void SetObject(TagControl _tagControl)
        {
            List<TagControl> _tags = new List<TagControl>();
            _tags.Add(_tagControl);
            SetObjects(_tags);
        }

        public void SetObjects(List<TagControl> _tagControls)
        {
            tags.Clear();
            tags.AddRange(_tagControls);
            this.OnTagsChanged(new EventArgs());
        }

        public void AddObject(TagControl _tagControl)
        {
            List<TagControl> _tags = new List<TagControl>();
            _tags.Add(_tagControl);
            AddObjects(_tags);
        }

        public void AddObjects(List<TagControl> _tagControls)
        {
            tags.AddRange(_tagControls);
            this.OnTagsChanged(new EventArgs(), TagsChangedType.Add);
        }

        #endregion

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            ControlPaint.DrawBorder3D(e.Graphics, base.ClientRectangle, Border3DStyle.SunkenInner);
            textBoxNewTag.Focus();
        }

        #region EventHandlers

        private void textBoxNewTag_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 9 || e.KeyChar == 13)
            {
                if (((TextBox)sender).Text.Trim().Replace("\t", "") != "")
                {
					TagBL tbl = new TagBL();

					//Search for a tag already added
					Tag tag = tbl.GetByName(((TextBox)sender).Text.Trim());

					//If doesn't exist
					if (tag == null)
					{
						tag = new Tag();
						tag.Name = ((TextBox)sender).Text.Trim();

						if (tbl.Save(tag))
							tag = tbl.GetLastAdded();
					}

					TagControl newtag = new TagControl(tag.Name);
					newtag.Tag = tag;

                    AddObject(newtag);
                    ((TextBox)sender).Text = "";
                    e.Handled = true;
                }
            }
        }

        #endregion
    }
}